﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8" />
    <title>Ping: Ping.cs Source File</title>
    <link href="/Content/Help.css" rel="stylesheet" type="text/css" />
    <link href="/Content/css/tabs.css" rel="stylesheet" type="text/css" />
    <link href="/Content/css/doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div id="header">
        <img id="logo" alt="StateForge" src="/Content/Images/StateForgeLogo.png" />
        <div id="title">
            <h1>
                C# examples using state machines generated by StateBuilderDotNet
            </h1>
            <h3>
                Ping: Ping.cs Source File
            </h3>
        </div>
        <div id="menucontainer">
            <ul id="menu">
                <li><a href="/">Home</a></li>
                <li><a href="/Home/Download">Downloads</a></li>
                <li><a href="/Help/state-machine-tools.aspx">Help</a></li>
                <li><a href="/Home/Faq">F.A.Q</a></li>
                <li><a href="/Home/About">Contact us</a></li>
            </ul>
        </div>
    </div>
<!-- Generated by Doxygen 1.7.4 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
    </ul>
  </div>
<div class="header">
  <div class="headertitle">
<div class="title">Ping.cs</div>  </div>
</div>
<div class="contents">
<a href="_ping_8cs.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 ﻿
<a name="l00002"></a><a class="code" href="namespace_state_forge_1_1_examples_1_1_ping.html">00002</a> <span class="keyword">namespace </span>StateForge.Examples.<a class="code" href="class_state_forge_1_1_examples_1_1_ping_1_1_ping.html" title="Ping.">Ping</a>
<a name="l00003"></a>00003 {
<a name="l00004"></a>00004     <span class="keyword">using</span> System;
<a name="l00005"></a>00005     <span class="keyword">using</span> StateForge.StateMachine;
<a name="l00006"></a>00006     <span class="keyword">using</span> System.Diagnostics;
<a name="l00007"></a>00007     <span class="keyword">using</span> System.Threading;
<a name="l00008"></a>00008     <span class="keyword">using</span> System.Text;
<a name="l00009"></a>00009     <span class="keyword">using</span> System.Net.NetworkInformation;
<a name="l00010"></a>00010 
<a name="l00014"></a><a class="code" href="class_state_forge_1_1_examples_1_1_ping_1_1_ping.html">00014</a>     <span class="keyword">public</span> <span class="keyword">partial class </span><a class="code" href="class_state_forge_1_1_examples_1_1_ping_1_1_ping.html" title="Ping.">Ping</a>
<a name="l00015"></a>00015     {
<a name="l00016"></a><a class="code" href="class_state_forge_1_1_examples_1_1_ping_1_1_ping.html#a3b640bf63fa2742e4793c6a205e181f1">00016</a>         <span class="keyword">public</span> <span class="keywordtype">double</span> Timeout { <span class="keyword">get</span>; <span class="keyword">set</span>; }
<a name="l00017"></a><a class="code" href="class_state_forge_1_1_examples_1_1_ping_1_1_ping.html#ac86d7dee47f8f610b1cb25dcd9d1816e">00017</a>         <span class="keyword">public</span> String Target { <span class="keyword">get</span>; <span class="keyword">set</span>; }
<a name="l00018"></a><a class="code" href="class_state_forge_1_1_examples_1_1_ping_1_1_ping.html#a9d0425fd73b064dc59a4ea6721b69007">00018</a>         <span class="keyword">public</span> <span class="keywordtype">int</span> Count { <span class="keyword">get</span>; <span class="keyword">set</span>; }
<a name="l00019"></a><a class="code" href="class_state_forge_1_1_examples_1_1_ping_1_1_ping.html#a493b31d0e1c6e82d570f4a47261459c8">00019</a>         <span class="keyword">public</span> <span class="keywordtype">int</span> Rx { <span class="keyword">get</span>; <span class="keyword">set</span>; }
<a name="l00020"></a><a class="code" href="class_state_forge_1_1_examples_1_1_ping_1_1_ping.html#a7ec64462dcb153d765c478d058d1c7d5">00020</a>         <span class="keyword">public</span> <span class="keywordtype">int</span> Tx { <span class="keyword">get</span>; <span class="keyword">set</span>; }
<a name="l00021"></a>00021 
<a name="l00022"></a><a class="code" href="class_state_forge_1_1_examples_1_1_ping_1_1_ping.html#a7adc8c44ef031b1b9aa0f394cf040b67">00022</a>         <span class="keyword">private</span> PingContext <a class="code" href="class_state_forge_1_1_examples_1_1_ping_1_1_ping.html#a7adc8c44ef031b1b9aa0f394cf040b67">context</a>;
<a name="l00023"></a><a class="code" href="class_state_forge_1_1_examples_1_1_ping_1_1_ping.html#a5f697343d4b6f2c89adf51ad9618ec7d">00023</a>         <span class="keyword">private</span> <span class="keyword">static</span> readonly <span class="keywordtype">string</span> traceName = <span class="stringliteral">&quot;Ping&quot;</span>;
<a name="l00024"></a><a class="code" href="class_state_forge_1_1_examples_1_1_ping_1_1_ping.html#a9432c160689a1e533e1037502dd59a01">00024</a>         <span class="keyword">private</span> <span class="keyword">static</span> TraceSource ts = <span class="keyword">new</span> TraceSource(traceName);
<a name="l00025"></a><a class="code" href="class_state_forge_1_1_examples_1_1_ping_1_1_ping.html#a0ee5fedb00c437de9de0156cfe2b6cf0">00025</a>         <span class="keyword">private</span> <span class="keyword">static</span> AutoResetEvent autoResetEvent = <span class="keyword">new</span> AutoResetEvent(<span class="keyword">false</span>);
<a name="l00026"></a>00026 
<a name="l00027"></a><a class="code" href="class_state_forge_1_1_examples_1_1_ping_1_1_ping.html#a47cc42c8cea76f5fadfd94019264f4e2">00027</a>         System.Net.NetworkInformation.Ping pingSender = <span class="keyword">new</span> System.Net.NetworkInformation.Ping();
<a name="l00028"></a><a class="code" href="class_state_forge_1_1_examples_1_1_ping_1_1_ping.html#a9101124f2c1d70ab83ee12f0a8e12308">00028</a>         PingOptions packetOptions = <span class="keyword">new</span> PingOptions(50, <span class="keyword">true</span>);
<a name="l00029"></a><a class="code" href="class_state_forge_1_1_examples_1_1_ping_1_1_ping.html#a55609dbfd44678f7fd5695112c42957a">00029</a>         byte[] packetData = Encoding.ASCII.GetBytes(<span class="stringliteral">&quot;................................&quot;</span>);
<a name="l00030"></a>00030 
<a name="l00031"></a><a class="code" href="class_state_forge_1_1_examples_1_1_ping_1_1_ping.html#adeda07c6acc07ff45cb62e0f6ae7009a">00031</a>         <span class="keyword">public</span> <a class="code" href="class_state_forge_1_1_examples_1_1_ping_1_1_ping.html" title="Ping.">Ping</a>()
<a name="l00032"></a>00032         {
<a name="l00033"></a>00033             pingSender.PingCompleted += <span class="keyword">new</span> PingCompletedEventHandler(PingComplete);
<a name="l00034"></a>00034             this.context = <span class="keyword">new</span> PingContext(<span class="keyword">this</span>);
<a name="l00035"></a>00035             this.context.Observer = ObserverTrace.Instance(traceName);
<a name="l00036"></a>00036             this.context.EndHandler += <span class="keyword">new</span> EventHandler&lt;EventArgs&gt;(StateMachineEnd);
<a name="l00037"></a>00037             this.context.EnterInitialState();
<a name="l00038"></a>00038             Reset();
<a name="l00039"></a>00039         }
<a name="l00040"></a>00040 
<a name="l00041"></a>00041         <span class="comment">//TODO HEEFRE</span>
<a name="l00042"></a><a class="code" href="class_state_forge_1_1_examples_1_1_ping_1_1_ping.html#ab98997fbc52e65a6e49fb67b1f343395">00042</a>         <span class="keyword">public</span> <span class="keywordtype">void</span> DoCancel()
<a name="l00043"></a>00043         {
<a name="l00044"></a>00044             this.pingSender.SendAsyncCancel();
<a name="l00045"></a>00045         }
<a name="l00046"></a>00046 
<a name="l00047"></a><a class="code" href="class_state_forge_1_1_examples_1_1_ping_1_1_ping.html#a1273f29582718355eba6465aad3c9d48">00047</a>         <span class="keyword">internal</span> <span class="keywordtype">void</span> Send()
<a name="l00048"></a>00048         {
<a name="l00049"></a>00049             ts.TraceInformation(<span class="stringliteral">&quot;Ping: Send to {0}&quot;</span>, this.Target);
<a name="l00050"></a>00050             pingSender.SendAsync(this.Target, (<span class="keywordtype">int</span>)this.Timeout, <span class="keyword">this</span>);
<a name="l00051"></a>00051         }
<a name="l00052"></a>00052 
<a name="l00053"></a><a class="code" href="class_state_forge_1_1_examples_1_1_ping_1_1_ping.html#add1d8cd6f73938c1a97d702ca26ec601">00053</a>         <span class="keyword">internal</span> <span class="keywordtype">void</span> PingComplete(<span class="keywordtype">object</span> sender, PingCompletedEventArgs e)
<a name="l00054"></a>00054         {
<a name="l00055"></a>00055             <span class="keywordflow">if</span> (e.Cancelled)
<a name="l00056"></a>00056             {
<a name="l00057"></a>00057                 ts.TraceInformation(<span class="stringliteral">&quot;Ping was canceled&quot;</span>);
<a name="l00058"></a>00058 
<a name="l00059"></a>00059             }
<a name="l00060"></a>00060 
<a name="l00061"></a>00061             <span class="keywordflow">else</span> <span class="keywordflow">if</span> (e.Error != null)
<a name="l00062"></a>00062             {
<a name="l00063"></a>00063                 ts.TraceEvent(TraceEventType.Error, 1, <span class="stringliteral">&quot;An error occured: {0}&quot;</span>, e.Error);
<a name="l00064"></a>00064                 this.context.EvError();
<a name="l00065"></a>00065             }
<a name="l00066"></a>00066 
<a name="l00067"></a>00067             <span class="keywordflow">else</span>
<a name="l00068"></a>00068             {
<a name="l00069"></a>00069                 PingReply pingResponse = e.Reply;
<a name="l00070"></a>00070                 ShowPingResults(pingResponse);
<a name="l00071"></a>00071             }
<a name="l00072"></a>00072         }
<a name="l00073"></a>00073 
<a name="l00074"></a><a class="code" href="class_state_forge_1_1_examples_1_1_ping_1_1_ping.html#a99abd067b774452cdf12d977950cb9c9">00074</a>         <span class="keyword">public</span> <span class="keywordtype">void</span> ShowPingResults(PingReply pingResponse)
<a name="l00075"></a>00075         {
<a name="l00076"></a>00076             <span class="keywordflow">if</span> (pingResponse == null)
<a name="l00077"></a>00077             {
<a name="l00078"></a>00078                 ts.TraceEvent(TraceEventType.Warning, 1, <span class="stringliteral">&quot;There was no response&quot;</span>);
<a name="l00079"></a>00079                 <span class="keywordflow">return</span>;
<a name="l00080"></a>00080             }
<a name="l00081"></a>00081             <span class="keywordflow">else</span> <span class="keywordflow">if</span> (pingResponse.Status == IPStatus.Success)
<a name="l00082"></a>00082             {
<a name="l00083"></a>00083                 ts.TraceEvent(TraceEventType.Information, 1, <span class="stringliteral">&quot;Reply from {0}  RTT {1}&quot;</span>,
<a name="l00084"></a>00084                     pingResponse.Address.ToString(),
<a name="l00085"></a>00085                     pingResponse.RoundtripTime);
<a name="l00086"></a>00086                 this.context.EvPingReply();
<a name="l00087"></a>00087             }
<a name="l00088"></a>00088             <span class="keywordflow">else</span>
<a name="l00089"></a>00089             {
<a name="l00090"></a>00090                 ts.TraceEvent(TraceEventType.Warning, 1, <span class="stringliteral">&quot;Ping was unsuccessful, ip status {0}&quot;</span>, pingResponse.Status);
<a name="l00091"></a>00091                 this.context.EvError();
<a name="l00092"></a>00092             }
<a name="l00093"></a>00093         }
<a name="l00094"></a>00094 
<a name="l00095"></a><a class="code" href="class_state_forge_1_1_examples_1_1_ping_1_1_ping.html#accc95ebb4741f480bd3e832bfaeed55a">00095</a>         <span class="keyword">private</span> <span class="keywordtype">void</span> Reset()
<a name="l00096"></a>00096         {
<a name="l00097"></a>00097             this.Target = <span class="stringliteral">&quot;127.0.0.1&quot;</span>;
<a name="l00098"></a>00098             this.Timeout = 1000; <span class="comment">/* msec */</span>
<a name="l00099"></a>00099             this.Count = 5;
<a name="l00100"></a>00100             this.Rx = 0;
<a name="l00101"></a>00101             this.Tx = 0;
<a name="l00102"></a>00102         }
<a name="l00103"></a>00103 
<a name="l00104"></a><a class="code" href="class_state_forge_1_1_examples_1_1_ping_1_1_ping.html#a07b9a37bc1c0293a6c8f02671fe334f8">00104</a>         <span class="keyword">public</span> <span class="keywordtype">void</span> PrintStatistics()
<a name="l00105"></a>00105         {
<a name="l00106"></a>00106             Console.WriteLine(<span class="stringliteral">&quot;Ping to {0}  attempt {1} of {2}, error: {3}&quot;</span>, this.Target, this.Tx, this.Count, this.Tx - this.Rx);
<a name="l00107"></a>00107         }
<a name="l00108"></a>00108 
<a name="l00109"></a><a class="code" href="class_state_forge_1_1_examples_1_1_ping_1_1_ping.html#a0f20a41e749a4052eace665ca3f3cf40">00109</a>         <span class="keyword">private</span> <span class="keywordtype">void</span> StateMachineEnd(<span class="keywordtype">object</span> sender, EventArgs e)
<a name="l00110"></a>00110         {
<a name="l00111"></a>00111             ts.TraceInformation(<span class="stringliteral">&quot;State machine has ended&quot;</span>);
<a name="l00112"></a>00112             autoResetEvent.Set();
<a name="l00113"></a>00113         }
<a name="l00114"></a>00114 
<a name="l00115"></a><a class="code" href="class_state_forge_1_1_examples_1_1_ping_1_1_ping.html#a0b0c6bc86532d61fa5827ece9c3aa4d2">00115</a>         <span class="keyword">static</span> <span class="keywordtype">void</span> Main(<span class="keywordtype">string</span>[] args)
<a name="l00116"></a>00116         {
<a name="l00117"></a>00117             <a class="code" href="class_state_forge_1_1_examples_1_1_ping_1_1_ping.html" title="Ping.">Ping</a> ping = <span class="keyword">new</span> <a class="code" href="class_state_forge_1_1_examples_1_1_ping_1_1_ping.html" title="Ping.">Ping</a>();
<a name="l00118"></a>00118             ping.<a class="code" href="class_state_forge_1_1_examples_1_1_ping_1_1_ping.html#ac86d7dee47f8f610b1cb25dcd9d1816e">Target</a> = <span class="stringliteral">&quot;127.0.0.1&quot;</span>;
<a name="l00119"></a>00119             ping.StartPing();
<a name="l00120"></a>00120             autoResetEvent.WaitOne();
<a name="l00121"></a>00121             ts.TraceInformation(<span class="stringliteral">&quot;Ping has ended&quot;</span>);
<a name="l00122"></a>00122             Environment.Exit(0);
<a name="l00123"></a>00123         }
<a name="l00124"></a>00124     }
<a name="l00125"></a>00125 }
</pre></div></div>
</div>
﻿<div id="footer">
    <hr />
    <p>
        Copyright &copy; 2011 StateForge.</p>
    <hr />
</div>
</body> </html> 
